package com.shop.shoporder.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.shop.shoporder.entity.Goods;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.entity.order.ViewSalesStatisticsProduct;
import com.shop.shoporder.mapper.order.ViewSalesStatisticsProductMapper;
import com.shop.shoporder.service.GoodsService;
import com.shop.shoporder.common.Encrypt;
import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/Goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    ViewSalesStatisticsProductMapper  viewSalesStatisticsProductMapper;
    /**
     * 读取商品
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/List", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response List(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            //try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:"+AID);

                //将接收到的数据转化为对象
                Goods goods1 = JSON.parseObject(res, new TypeReference<Goods>() {
                });

                PageHelper.startPage(goods1.getPage(), goods1.getLimit());

                //读取数据表
                List<Goods> listGoods = goodsService.selectAll(goods1);
                if (listGoods != null) {

                    PageInfo<Goods> pageInfo=new PageInfo<>(listGoods);
                    int count=(int)pageInfo.getTotal();

                    //return new Response(MessageConstants.MSG_SUCCESS, listGoods, listGoods.size(), "", request);
                    return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), count, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
                }
//            } catch (Exception ex) {
//                System.out.println(ex.getMessage());
//                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
//            }
        }
    }

    /**
     * 读取数据
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Self", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response Self(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:"+AID);
                //将接收到的数据转化为对象
                Goods goods1 = JSON.parseObject(res, new TypeReference<Goods>() {
                });

                Goods goods = goodsService.selectByPrimaryKey(goods1.getId());
                if(goods!=null) {
                    return new Response(MessageConstants.MSG_SUCCESS, goods, 1, "", request);
                }
                else
                {
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误2", request);
                }
            }
            catch (Exception ex)
            {
                System.out.println(ex.getMessage());
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }
    }

    /**
     * 添加数据
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response Add(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
 //           try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:"+AID);
                //将接收到的数据转化为对象
                Goods goods = JSON.parseObject(res, new TypeReference<Goods>() {
                });

                int row = goodsService.insertSelective(goods);

                if (row > 0) {
                    return new Response(MessageConstants.MSG_SUCCESS, goods, row, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "添加数据错误B", request);
                }
//            }
//            catch (Exception ex)
//            {
//                System.out.println(ex.getMessage());
//                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "添加数据错误", request);
//            }
        }
    }

    /**
     * 编辑数据
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Set", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response Set(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:"+AID);
                //将接收到的数据转化为对象
                Goods goods = JSON.parseObject(res, new TypeReference<Goods>() {
                });
                int row = goodsService.updateByPrimaryKeySelective(goods);
                if (row > 0) {
                    return new Response(MessageConstants.MSG_SUCCESS, goods, row, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "更新数据错误B", request);
                }
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "更新数据错误A", request);
            }
        }
    }


    @ResponseBody
    @RequestMapping(value = "/Encrypt")
    public String Encrypt(HttpServletRequest request) {
        String key=request.getParameter("key");
        String keyStr = Encrypt.encrypt(key);
        String key1 = Encrypt.decrypt(keyStr);
        return keyStr+"---"+key1;
    }

    @ResponseBody
    @RequestMapping(value = "/getProductSales", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response getProductSales(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:"+AID);
                System.out.println("接受参数："+res);
                JSONObject param = JSONObject.parseObject(res);
                int page =0;
                int limit =10;
                //Map<String,String> mapPa=new HashMap<>();
                String supplier=(param.get("supplier"))==null?"":(String) param.get("supplier");
                String newpr=(param.get("newpr"))==null?"0":(String) param.get("newpr");
                String productName=(param.get("productName"))==null?"0":(String) param.get("productName");
//                mapPa.put("supplier",supplier);
//                mapPa.put("newpr",newpr);
                int Intlimit=InterfaceVer.VerNumber((String) param.get("limit"));
                int Intpage=InterfaceVer.VerNumber((String) param.get("page"));
                page=Intpage;
                limit=Intlimit;
                PageHelper.startPage(page, limit);
                //读取数据表
                List<ViewSalesStatisticsProduct> list=viewSalesStatisticsProductMapper.selectAll(supplier,newpr,productName);
                if (list != null&&list.size()>0) {
                    PageInfo<ViewSalesStatisticsProduct> pageInfo=new PageInfo<>(list);
                    int count=(int)pageInfo.getTotal();
                    return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), count, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }
    }

}